<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="10">
        <el-col :span="10">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            @keydown.native.enter="getGoodsList"
            @clear="getGoodsList"
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格数据区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column
          prop="goods_price"
          width="100"
          label="商品价格(元)"
        ></el-table-column>
        <el-table-column
          prop="goods_weight"
          width="70"
          label="商品重量"
        ></el-table-column>
        <el-table-column prop="add_time" width="140" label="创建时间">
          <template v-slot="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column width="130" label="操作">
          <template v-slot="scope">
            <!-- 修改 -->
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="primary"
              @click="showEditDialog(scope.row.goods_id)"
            ></el-button>
            <!-- 删除 -->
            <el-button
              size="mini"
              icon="el-icon-delete"
              type="danger"
              @click="removeGoodsById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[8, 15, 20, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
    <!-- 修改的对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <!-- 内容区域 -->
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
         <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
         <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 添加商品表单数据
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 8
      },
      // 修改商品的数据表单
      editForm: {
        goods_name: '',
        goods_price: '',
        goods_weight: ''
      },
      // 商品列表
      goodsList: [],
      // 总数据条数
      total: 0,
      // 控制修改商品对话框的显示与隐藏
      editDialogVisible: false,
      // 修改表单的验证规则
      editFormRules: {
        goods_name: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        goods_price: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        goods_weight: [{ required: true, message: '请输入活动名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 根据分页获取商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 分页的条数变化
    handleSizeChange(newSize) {
      // 将新的数据更新到表单中
      this.queryInfo.pagesize = newSize
      // 重新获取数据
      this.getGoodsList()
    },
    // 页码发生变化
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 根据id删除商品
    async removeGoodsById(goodsId) {
      const confirmRes = await this.$confirm('确认删除该商品？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes === 'cancel') return this.$message.info('用户取消了操作')
      // 发送请求
      const { data: res } = await this.$http.delete(`goods/${goodsId}`)
      // 判断结果
      if (res.meta.status !== 200) return this.$message.error('删除商品失败')
      this.$message.success('删除商品成功')
      // 刷新列表
      this.getGoodsList()
    },
    // 显示修改按钮的对话框
    async showEditDialog(goodsId) {
      console.log(goodsId)
      // 根据id查询数据
      const { data: res } = await this.$http.get(`goods/${goodsId}`)
      if (res.meta.status !== 200) { return this.$message.error('获取商品信息失败') }
      this.editForm = res.data
      // 显示对话框
      this.editDialogVisible = true
    },
    // 修改对话框关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    // 修改商品
    edit() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return false
        const { data: res } = await this.$http.put(`goods/${this.editForm.goods_id}`, this.editForm)
        if (res.meta.status !== 200) return this.$message.error('修改商品信息失败')
        // 刷新列表
        this.getGoodsList()
        // 关闭对话框
        this.editDialogVisible = false
      })
    },
    // 点击添加按钮 跳转添加页面
    goAddPage() {
      this.$router.push('goods/add')
    }
  }
}
</script>

<style lang="less" scoped></style>
